$(function () {
    $("#jqGrid").Grid({
        url: '../productrelateshop/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '产品名称', name: 'productName', index: 'product_name', width: 80},
			{label: '产品ID', name: 'productId', index: 'product_id',  hidden: true},
			{label: '商铺Id', name: 'shopId', index: 'shop_id', hidden: true},
			{label: '产品类型', name: 'productTypeName', index: 'product_type_name'},
			{label: '商铺名称', name: 'shopName', index: 'shop_name', width: 80},
			{label: '库存数量', name: 'stockNum', index: 'stock_num', width: 80},
			{label: '状态', name: 'status', index: 'status', width: 80 ,formatter: function (value, options, row) {
                return value === 0 ?
                    '<span class="label label-success">启用</span>' :
                    '<span class="label label-danger">禁用</span>';}},
			{label: '创建时间', name: 'createTime', index: 'create_time', width: 80,formatter: function (value) {
                return transDate(value, 'yyyy-MM-dd hh:mm:ss');
            }},
			{label: '修改时间', name: 'updateTime', index: 'update_time', width: 80,formatter: function (value) {
                return transDate(value, 'yyyy-MM-dd hh:mm:ss');
            }}]
    });
    vm.getshopss();
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: 1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		productRelateShop: {
            goodId:0,
            productId:0,
            buildingId:0,
            productTypeId:0
        },
        goodss: [],
        goods: [],
		shopss:[],
		shops:[],
        products:[],
        shop:[],
        selectData: {},
        productTypes: [],
		q: {
		    shopId: ''
		},
        ruleValidate: {
            shopss: [
                {required: true, message: '店铺不能为空', trigger: 'blur'}
            ],
            goodss: [
                {required: true, message: '商品不能为空', trigger: 'blur'}
            ]
        },
		areaBuilding:{
            name: null,
            id:0,
            type: 1}
	},
	methods: {
		query: function () {
		        vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.productRelateShop = {};
            vm.getProductTypes();
		},
		update: function (event) {
		    debugger;
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getProductTypes();
            // vm.getGoodss();
            vm.getshopss();
            vm.getInfo(id);
		},
        //根据区域id选择店铺列表
        getShopByAreaId: function (id) {
            //加载店铺列表
            Ajax.request({
                url: "../shops/queryByAreaId/"+id,
                param:JSON.stringify(id),
                async: true,
                successCallback: function (r) {
                    vm.shopss = r.list;
                }
            });
        },
        //获取所有产品类型
        getProductTypes: function () {
            Ajax.request({
                url: "../category/queryAll",
                async: true,
                successCallback: function (r) {
                    vm.productTypes = r.list;
                }
            });
        },
        areaTree: function () {
            openWindow({
                title: "选择菜单",
                area: ['300px', '450px'],
                content: jQuery("#areaLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    if(node[0].type !== 3){
                        alert('请选择具体到寝室楼', function (index) {
                        });
                    }else{
                        //选择上级菜单
                        vm.areaBuilding.id = node[0].id;
                        vm.areaBuilding.name = node[0].name;
                        vm.getShopByAreaId(vm.areaBuilding.id);
                        layer.close(index);
                    }
                }
            });
        },
        getAreaTree: function (menuId) {
            //加载菜单树
            Ajax.request({
                url: "../regionrelatebedroom/select",
                async: true,
                successCallback: function (r) {
                    debugger;
                    ztree = $.fn.zTree.init($("#areaTree"), setting, r.bedroomEntities);
                    var node = ztree.getNodeByParam("id", vm.productRelateShop.buildingId);
                    if (node) {
                        ztree.selectNode(node);
                        vm.areaBuilding.name = node.name;
                        vm.areaBuilding.id = node.id;
                        vm.areaBuilding.type=node.type;
                    }
                }
            });
        },
		saveOrUpdate: function (event) {
            let url = vm.productRelateShop.id == null ? "../productrelateshop/save" : "../productrelateshop/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.productRelateShop),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../productrelateshop/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo:function(id){
		    Ajax.request({
                url: "../productrelateshop/info/"+id,
                async: true,
                successCallback: function (r) {
                    debugger;
                    vm.productRelateShop = r.productRelateShop;
                }
            })
            },
        getGoodss:function (event) {
            Ajax.request({
                url: "../goods/queryAll",
                params:JSON.stringify(vm.productRelateShop),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    vm.goodss = r.list;
                }
            });
        },
		getshopss:function () {
			Ajax.request({
                url: "../shops/queryAll/",
                async: true,
                successCallback: function (r) {
                    vm.shopss = r.list;
                }
			})
        },
        haveValue:function (event) {
            if(vm.productRelateShop.productId!==0&&vm.productRelateShop.productId!==''){
                // vm.getProduct();
            }
        },
        getProduct:function () {
		        debugger;
		        Ajax.request({
                url: "../product/queryByGoodsId/"+vm.productRelateShop.goodId,
                async: true,
                successCallback: function (r) {
                    vm.products = r.list;
                }
            })},
		reload: function (event) {
                vm.showList = true;
                let page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    postData: {'shopId': vm.q.shopId},
                    page: page
                }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                shopId: '',
            }
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	},
    created:function(){

        },
    watch: {
        'productRelateShop.productTypeId': {
            handler(newId) {
                debugger;
                if(newId!==0&&newId!==''){
                    vm.getGoods();
                }
            },
            deep: true
        },
        'productRelateShop.goodId': {
            handler() {
                debugger;
                if(vm.productRelateShop.goodId!==0&&vm.productRelateShop.goodId!==''){
                    vm.getProduct();
                    vm.getAreaTree();
                }
            },
            deep: true
        },
        'areaBuilding.id': {
            handler(newId) {
                debugger;
                //根据宿舍楼获取店铺
                vm.getShopByAreaId(newId);
                vm.productRelateShop.buildingId = newId;
            },
            deep: true
        }
    }

});